<?php require_once('../../../Connections/shopcarda1.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  mysql_select_db($database_shopcarda1, $shopcarda1);

for($i=0;$i<count($_POST['logo_name']);$i++){ 

$file_name = $_FILES['pic']['name'][$i];
//分割檔名
$fn_array=explode(".",$file_name);
//檔名
$mainName = $fn_array[0];
//副檔名	
$subName = $fn_array[1];

$files_upload=$_FILES['pic']['tmp_name'][$i];
$files_name= (int)(date("YmdHi"))+rand(0,99999);

// 複製上傳圖片到指定 images 目錄
copy($_FILES['pic']['tmp_name'][$i], "images/" .$files_name ."." .$subName); 
$pic_name = $files_name ."." .$subName;
if($_FILES['pic']['name'][$i]==""){
	$pic_name = "";
	}

  $insertSQL = sprintf("INSERT INTO supplier_brand (cm_id, logo_name, logo_name_en, pic) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($_POST['cm_id'], "int"),
                       GetSQLValueString($_POST['logo_name'][$i], "text"),
                       GetSQLValueString($_POST['logo_name_en'][$i], "text"),
                       GetSQLValueString($pic_name, "text"));

  $Result1 = mysql_query($insertSQL, $shopcarda1) or die(mysql_error());
  	   }

  $insertGoTo = "../admin_supplier_brand.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

$colname_company_member_r = "-1";
if (isset($_GET['cm_id'])) {
  $colname_company_member_r = $_GET['cm_id'];
}
mysql_select_db($database_shopcarda1, $shopcarda1);
$query_company_member_r = sprintf("SELECT cm_id FROM company_member WHERE cm_id = %s", GetSQLValueString($colname_company_member_r, "int"));
$company_member_r = mysql_query($query_company_member_r, $shopcarda1) or die(mysql_error());
$row_company_member_r = mysql_fetch_assoc($company_member_r);
$totalRows_company_member_r = mysql_num_rows($company_member_r);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文件</title>
<style type="text/css">
.style16 {font-size: 14px}
</style>
</head>
<script src="../../js/jquery.js" type="text/javascript"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
$("input#add").click(function(){addSpot(this); 
}); 
}); 
function addSpot(obj, sm) { 
$('div#spots').append( 
'<br /><div width="737"><span class="style16">品牌名稱</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + 
'<input name="logo_name[]" type="text" />' + 
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style16">品牌英文名稱</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + 
'<input name="logo_name_en[]" type="text" />' +
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +
'<input type="button" class="remove" value="-" /><br />' +
'<span class="style16">品牌形象圖</span>&nbsp;&nbsp;&nbsp;&nbsp;' +
'<input name="pic[]" type="file" />' +
'</label></div>') 
.find("input.remove").click(function(){ 
$(this).parent().remove(); 
});
}; 
</script> 
</head> 
<body>
<form action="<?php echo $editFormAction; ?>" method="POST" name="form1" id="form1" enctype="multipart/form-data"> 
<table align="center">
  <tr>
    <td>
<div id="spots"> 
<div style="background-color:#D5EBEF">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="button" id="add" name="add" value="+" />
<input name="cm_id" type="hidden" id="cm_id" value="<?php echo $row_company_member_r['cm_id']; ?>" />
</div>
<div width="737"><span class="style16">品牌名稱</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  <input name="logo_name[]" type="text" /> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style16">品牌英文名稱</span>&nbsp;&nbsp;&nbsp;&nbsp;
<input name="logo_name_en[]" type="text" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="button" class="remove" value="-" /><br />
<span class="style16">品牌形象圖</span>&nbsp;&nbsp;&nbsp;
<input name="pic[]" type="file" /></label>                       
</div>
</div>
    </td>
  </tr>
  <tr>
      <td align="center"><input type="submit" name="submit" id="submit" value="確定" />&nbsp;<input type="button" value="取消" onclick="window.close();"></td>
    </tr>
</table>
<input type="hidden" name="MM_insert" value="form1" />
</form>
</body> 
</html>
<?php
mysql_free_result($company_member_r);
?>
